#!/bin/bash
#sh uploadArgSetForEnv_pgyer.sh -pagyerDownJsonF "${Pgyer_Download_FILE_PATH}" -envJsonF "${ENVIRONMENT_FILE_PATH}"
#sh uploadArgSetForEnv_pgyer.sh -pagyerDownJsonF "../example_packing_info/app_info.json" -envJsonF "../example_packing_info/app_info.json"

echo "===========进入脚本$0==========="

exit_script() { # 退出脚本的方法，省去当某个步骤失败后，还去继续多余的执行其他操作
    exit 1
}


JQ_EXEC=`which jq`

# shell 参数具名化
show_usage="args: [-pagyerDownJsonF, -envJsonF]\
                                  [--pgyer-download-json-file=, --environment-json-file=]"

while [ -n "$1" ]
do
        case "$1" in
                -pagyerDownJsonF|--pgyer-download-json-file) Pgyer_Download_FILE_PATH=$2; shift 2;;
                -envJsonF|-environment-json-file) ENVIRONMENT_FILE_PATH=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

# 当前【shell脚本】的工作目录
# $PWD代表获取当前路径，当cd后，$PWD也会跟着更新到新的cd路径。这个和在终端操作是一样的道理的
CurrentDIR_Script_Absolute="$( cd "$( dirname "$0" )" && pwd )"
#echo "CurrentDIR_Script_Absolute=${CurrentDIR_Script_Absolute}"
#CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute}/..
CommonFun_HomeDir_Absolute=${CurrentDIR_Script_Absolute%/*} # 使用此方法可以避免路径上有..


if [ ! -f "${CommonFun_HomeDir_Absolute}/a_function.sh" ]; then
    PackageErrorCode=1
    PackageErrorMessage="引入方法文件:《 source ${CommonFun_HomeDir_Absolute}/a_function.sh ${CommonFun_HomeDir_Absolute} 》失败，请检查"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi
source ${CommonFun_HomeDir_Absolute}/a_function.sh ${CommonFun_HomeDir_Absolute}

if [ -z "${Pgyer_Download_FILE_PATH}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="执行本脚本需要传入 -pagyerDownJsonF 参数，请检查您的执行命令！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
elif [ ! -f "${Pgyer_Download_FILE_PATH}" ];then
    PackageErrorCode=1
    PackageErrorMessage="您的Pgyer_Download_FILE_PATH=${Pgyer_Download_FILE_PATH}文件不存在，请检查！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi

if [ -z "${ENVIRONMENT_FILE_PATH}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="执行本脚本需要传入 -envJsonF 参数，请检查您的执行命令！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
elif [ ! -f "${ENVIRONMENT_FILE_PATH}" ];then
    PackageErrorCode=1
    PackageErrorMessage="您的ENVIRONMENT_FILE_PATH=${ENVIRONMENT_FILE_PATH}文件不存在，请检查！"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script
fi


source ${CommonFun_HomeDir_Absolute}/a_source_env.sh -appInfoF "${ENVIRONMENT_FILE_PATH}" # 获取 BRANCH\PlatformType\PackageTargetType\PackageNetworkType

ShortBranceName=${BRANCH##*/}
echo "ShortBranceName=$ShortBranceName"
echo "------PlatformType:${PlatformType}"
echo "------PackageTargetType:${PackageTargetType}"
echo "------PackageNetworkType:${PackageNetworkType}"


pgyerParamsRootMapString=$(cat ${Pgyer_Download_FILE_PATH} | ${JQ_EXEC} -r ".package_pgyer_params")
if [ "${pgyerParamsRootMapString}" == "null" ] || [ -z "${pgyerParamsRootMapString}" ]; then
    PackageErrorCode=1
    PackageErrorMessage="你的${Pgyer_Download_FILE_PATH}中未添加package_pgyer_params参数，会导致即使打包成功，蒲公英也无法上传成功"
    updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
    exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
fi



# 获取指定环境的所有打包依据
function get_network_PgyerRootMapString() {
    # 是map时候
    #network_PgyerRootMapString=$(echo ${pgyerParamsRootMapString} | ${JQ_EXEC} -r --arg networkKey "$networkKey" '.[].$networkKey')

    # 是数组时候
    network_PgyerRootMapString=$(echo ${pgyerParamsRootMapString} | ${JQ_EXEC} -r ".[] | select(.network==\"${PackageNetworkType}\")")
    # echo "network_PgyerRootMapString=${network_PgyerRootMapString}"
    if [ "${network_PgyerRootMapString}" == "null" ] || [ -z "${network_PgyerRootMapString}" ]; then
        PackageErrorCode=1
        PackageErrorMessage="获取 ${Pgyer_Download_FILE_PATH} 文件中 package_pgyer_params  里环境为 ${PackageNetworkType} 环境的值失败，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script # 这是严重错误，所以直接退出本脚本，没必要继续执行
    fi

    sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f "${Pgyer_Download_FILE_PATH}" -k "package_pgyer_params_current" -v "${network_PgyerRootMapString}"
    if [ $? != 0 ]; then
        # echo "❌Error：执行命令失败(更新package_pgyer_params_current)：《 sh ${CommonFun_HomeDir_Absolute}/update_json_file.sh -f \"${Pgyer_Download_FILE_PATH}\" -k \"package_pgyer_params_current\" -v \"${network_PgyerRootMapString}\" 》"
        PackageErrorCode=1
        PackageErrorMessage="更新 package_pgyer_params_current 属性失败，请检查！"
        updatePackageErrorCodeAndMessage ${PackageErrorCode} "${PackageErrorMessage}"
        exit_script "更新 package_pgyer_params_current 属性失败，请检查！"
    fi
}

get_network_PgyerRootMapString